package com.ham.mud.commands;

import com.ham.mud.Colors;
import com.ham.mud.ServerConnection;
import com.ham.mud.commands.spells.Spell;

import java.util.*;

/**
 * Created by hlucas on Jun 30, 2011 at 11:39:55 AM
 */
public class SpellsCommand extends PlayerCommand {
    @Override
    public void execute(ServerConnection connection, String[] params) {
        connection.printStart("Your spells:");
        connection.println(" ---------------------------------------------");
        int playerLevel = connection.getPlayer().getLevel();
        Map<Integer, List<Spell>> potentialSpells = connection.getPlayer().getMudClass().getSpells();
        List<Integer> keysList = new ArrayList<Integer>(potentialSpells.keySet());

        Collections.sort(keysList, new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        });

        for(Integer key : keysList) {
            String active = "Novice";
            if(key > playerLevel) {
                active = "------";
            }

            String levelString = "|   Level " + key + ":  ";
            StringBuilder sb = new StringBuilder();
            sb.append("|");
            int length = levelString.length();
            for(int i = 1; i < length; i++) {
                sb.append(" ");
            }
            String bufferString = sb.toString();
            List<Spell> spells = potentialSpells.get(key);
            boolean isFirst = true;
            for(Spell spell : spells) {
                if(isFirst) {
                    connection.println(levelString + Colors.getSpaces(3, String.valueOf(key)) + spell.getName() + Colors.getSpaces(20, spell.getName()) + active + Colors.getSpaces(10, active) + "|");
                    isFirst = false;
                } else {
                    connection.println(bufferString + Colors.getSpaces(3, String.valueOf(key)) + spell.getName() + Colors.getSpaces(20, spell.getName()) + active + Colors.getSpaces(10, active) + "|");
                }
            }
        }
        connection.println(" ---------------------------------------------");
        connection.printEnd();
    }

    @Override
    protected String getCommandName() {
        return "spells";
    }
}
